package mysale.web;

import com.mysql.cj.Session;
import mysale.pojo.User;
import mysale.service.UserService;
import mysale.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author SnakeWoodMan
 * @create 2020-12-23 21:21
 */
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String operation = request.getParameter("op");
        UserServiceImpl userService = new UserServiceImpl();
        if("login".equals(operation)){
            String userName = request.getParameter("username");
            String password = request.getParameter("password");
            String checkid = (String) request.getSession().getAttribute("randCheckCode");
            System.out.println("checkid:"+checkid);
            User user = userService.login(userName, password);
            String checkcode = request.getParameter("checkCode");
            System.out.println("checkcode = "+checkcode);
            if(checkcode.equals("")||checkcode==null){
                request.setAttribute("msg","请输入验证码");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }else{
                if(! checkcode.equals(checkid)){
                    request.setAttribute("msg","验证码错误，请重新输入！");
                    request.getRequestDispatcher("/login.jsp").forward(request, response);
                }
//
            }
            if(user!=null){
                request.getSession().setAttribute("user", user);
                System.out.println(user.getUsername());
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            }else {
                request.setAttribute("msg","账号或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }

        }else if ("regist".equals(operation)){
            String userName = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            User user = new User();
            user.setUsername(userName);
            user.setPassword(password);
            user.setEmail(email);
            boolean result = userService.regist(user);
            if(result){
                  response.sendRedirect(request.getContextPath()+"/registsuccess.jsp");
            }else {
                request.setAttribute("msg","账号已存在，请重新输入！");
                request.getRequestDispatcher("/regist.jsp").forward(request, response);
//                response.sendRedirect(request.getContextPath()+"/regist.jsp");
            }

        }else if(operation.equals("logout")){
            // 清空会话中的所有数据
            request.getSession().invalidate();
            try {
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        }

    }

